package com.final_project.zombiemenace.model;

import android.graphics.Point;

import com.kilobolt.framework.Input.TouchEvent;

public class Joystick {
	
	//Joystick borders
	private final int BOTTOM_JOYSTICK_BORDER;
	private final int TOP_JOYSTICK_BORDER;
	private final int RIGHT_JOYSTICK_BORDER;
	private final int LEFT_JOYSTICK_BORDER;
	
	//Joystick center coords
	private final int INIT_X_JOYSTICK;
	private final int INIT_Y_JOYSTICK;
	
	// Point of the Joystick
	private Point centerPointJoystick;
	
	//Joystick Constructor
	public Joystick(int top, int right, int bottom, int left, int initX, int initY){
		BOTTOM_JOYSTICK_BORDER = bottom;
		TOP_JOYSTICK_BORDER = top;
		RIGHT_JOYSTICK_BORDER = right;
		LEFT_JOYSTICK_BORDER = left;
		INIT_X_JOYSTICK = initX;
		INIT_Y_JOYSTICK = initY;
		centerPointJoystick = new Point(INIT_X_JOYSTICK, INIT_Y_JOYSTICK);
	}
	
	public void updateJoystick(TouchEvent event) {
				centerPointJoystick.x = event.x;
				centerPointJoystick.y = event.y;
				if (event.type == TouchEvent.TOUCH_DOWN) {
					setJoystickBounds();
				} else if (event.type == TouchEvent.TOUCH_DRAGGED) {
					setJoystickBounds();
				} else if (event.type == TouchEvent.TOUCH_UP) {
					resetJoystick();
				}
		}

	public void resetJoystick() {
		centerPointJoystick.x = INIT_X_JOYSTICK;
		centerPointJoystick.y = INIT_Y_JOYSTICK;
	}

	private void setJoystickBounds() {
		if (centerPointJoystick.x < LEFT_JOYSTICK_BORDER) {
			centerPointJoystick.x = LEFT_JOYSTICK_BORDER;
		}
		if (centerPointJoystick.x > RIGHT_JOYSTICK_BORDER) {
			centerPointJoystick.x = RIGHT_JOYSTICK_BORDER;
		}
		if (centerPointJoystick.y < TOP_JOYSTICK_BORDER) {
			centerPointJoystick.y = TOP_JOYSTICK_BORDER;
		}
		if (centerPointJoystick.y > BOTTOM_JOYSTICK_BORDER) {
			centerPointJoystick.y = BOTTOM_JOYSTICK_BORDER;
		}
	}
	
	public int getCenterPointX(){
		return this.centerPointJoystick.x;
	}
	
	public int getCenterPointY(){
		return this.centerPointJoystick.y;
	}
	
	public void setCenterPointX(int newCenterX){
		this.centerPointJoystick.x = newCenterX ;
	}
	
	public void setCenterPointY(int newCenterY){
		this.centerPointJoystick.y = newCenterY;
	}
	
	public int getInitX(){
		return this.INIT_X_JOYSTICK;
	}
	
	public int getInitY(){
		return this.INIT_Y_JOYSTICK;
	}
	
	public int getLeftBorder(){
		return this.LEFT_JOYSTICK_BORDER;
	}
	
	public int getRightBorder(){
		return this.RIGHT_JOYSTICK_BORDER;
	}
	
	public int getTopBorder(){
		return this.TOP_JOYSTICK_BORDER;
	}
	
	public int getBottomBorder(){
		return this.BOTTOM_JOYSTICK_BORDER;
	}
}
